<template>
    <div id="maxConnEdit" class="mt10">
        <div>
            <el-form ref="editForm" :model="editForm" class="formEmbedTable mt20" :rules="rules">
                <table>
                    <caption>Modify Connections</caption>
                    <tr>
                        <td>Channel</td>
                        <td>
                            <el-form-item prop="paramChannel">{{editForm.paramChannel}}</el-form-item>
                        </td>	
                        <td>Transaction code</td>
                        <td >
                            <el-form-item prop="paramCode">{{editForm.paramCode}}</el-form-item>
                        </td>					
                    </tr>
                    <tr>
                        <td>Connections</td>
                        <td >
                            <el-form-item prop="paramValue">
                                <el-input v-model="editForm.paramValue"></el-input>
                            </el-form-item>
                        </td>					
                    </tr>
                </table>
            </el-form>
        </div>
        <div class="btnArea mt20 tac">
            <el-button type="primary" @click="queryAuth('editForm')">Confirm</el-button>
            <el-button type="primary" @click="back()">Cancel</el-button>
        </div>
        <div>
            <ynet-authDialog ref="dialogTable"  @getAuthForm="getAuthForm"></ynet-authDialog>
        </div>
    </div>
</template>
<script>
 import ghbIcon from '@src/components/icon'
    export default {
        name:'maxConnEdit',
        components:{
            ghbIcon
        },
        props:['editData'],
        mounted(){
           this.editForm=$.extend(this.editForm,this.editData)
           console.log("editForm:"+JSON.stringify(this.editForm))
        },
        data(){
            return{
                dialogTableVisible:false,
                needAuth:'',
                authType:'',  
                authForm: { authUserNo: '', password: '',randomField:''},
                editForm:{
                    bsnCode:'IM0202',actionType:'1',paramValue:'',
                },
                rules:{
                    paramChannel:[
                        {required:true,message:'required',trigger:'blur'},
                    ],
                    paramCode:[
                        {required:true,message:'required',trigger:'blur'},
                        {validator:this.$tools.validator.isEngNum,trigger:'blur'},
                    ],
                    paramValue:[
                        {required:true,message:'required',trigger:'blur'},
                        {validator:this.$tools.validator.isInt,trigger:'blur'},
                    ],
                },
            }
        },
        methods:{
            back(){//返回到查询页面
                this.$emit('back')
            },
            getAuthForm(val){
                //获取授权数据
                this.authForm=val;
                console.log("val:"+JSON.stringify(val))
                this.editForm=$.extend(this.editForm,this.authForm)
                this.submit()  //自己页面提交给后台的.do方法
            },
            queryAuth(formName){//查询授权模式
                this.$refs[formName].validate((valid)=>{
                    if(valid){
                        let body={};body.bsnCode="IM0202";body.version="IM0202",
                        this.$tools.request(this,"IM010001Action.do",body).then(data=>{
                            if(data.body.errorCode==0){
                                if(data.body.needAuth==0){//无需授权
                                    this.submit()
                                }else if(data.body.needAuth==1 && data.body.authType==1){
                                    this.needAuth=1;
                                    this.authType=1;
                                    this.$refs["dialogTable"].show()
                                    // this.dialogTableVisible=true;
                                }else if( data.body.needAuth==1 && data.body.authType==2 ){
                                    this.needAuth=1;
                                    this.authType=2;
                                    this.submit()
                                }else{
                                    this.$alert('Authorization Type Error', {
                                        confirmButtonText: 'Confirm',
                                        callback: action => {
                                        }
                                    });
                                }
                            }else{
                                this.$alert("System Error Try again", "warning", {
                                    confirmButtonText: 'Confirm',
                                    callback: action => {
                                    }
                                });
                            }
                            
                        })
                    }
                })
                
			},
            submit(){
                console.log("editForm:"+JSON.stringify(this.editForm))
                this.$tools.request(this,"IM0202Action.do",this.editForm).then(data=>{
                    if(data.body.errorCode==0){
                        if(this.needAuth==1 && this.authType==2){
                            this.$message({
								type:'success',
								message:'Waiting for Authorized'
							})
                            this.$emit("back")
                        }else if(this.needAuth==1 && this.authType==1){
                            if(data.body.authStatus==1){
                                this.$message({
                                    type:'warning',
                                    message:'Authorship No. or password wrong ,please resume load',
                                    showClose:true,
                                })
                                this.$emit("back")
                            }else{
                                this.$message({
                                    type:'success',
                                    message:'Modify Success',
                                    showClose:true,
                                })
                                this.$emit("back")
                            }
                        }else{
                             this.$message({
                                type:'success',
                                message:'Delete Success',
                                showClose:true,
                            })
                            this.$emit("back")
                        }
                    }else{
                        this.$alert("System Error Try again", "warning",{
                            confirmButtonText: 'Confirm',
                            callback: action => {
                            }
                        })
                    }
                })
            },
        }
    }
</script>